CC=gcc
CXX=g++
EXTRADEFS=-DLINUX_OS \
	-DGIMI_NO_DATATYPE_INCLUDES
INCLUDES=-I. -I.. 
CFLAGS=-O0 -g3 -Wall $(INCLUDES) $(CE) \
	$(EXTRADEFS)
CPPFLAGS=-pg -O0 -g3 -Wall -Wextra -Weffc++ -Wunused -Wshadow $(INCLUDES) \
	`xml2-config --cflags` $(EXTRADEFS)
LD=g++
LDFLAGS=
LIBS=-lrt -lpthread `xml2-config --libs`

# Define COMPILE and LINK commands
COMPILEC=$(CC) $(CFLAGS) -c
COMPILECPP=$(CXX) $(CPPFLAGS) -c
LINK=$(LD) $(LDFLAGS) $(LIBS)

.PHONY: all clean


###############################################################################
###############################################################################
# To compile ALL examples

all:	settings_example \
	owndebug_example \
	binbag_example \
	threadandsync_example \
	databacklog_example \
	base64_example \
	bitOps_example \
	datatypesizes_example
###############################################################################
#################### Rules for different example programs #####################
###############################################################################

# Rule for SETTINGS EXAMPLE
SETTINGS_OBJECTS=../settings.opp settings_example.opp ../owndebug.o ../ownutils.o
settings_example: $(SETTINGS_OBJECTS)
	@$(LINK) -o $@ $^
	@echo "      [LD] $@"

# Rule for OWNDEBUG EXAMPLE
OWNDEBUG_OBJECTS=owndebug_example.opp ../owndebug.o ../ownutils.o
owndebug_example: $(OWNDEBUG_OBJECTS)
	@$(LINK) -o $@ $^
	@echo "      [LD] $@"

# Rule for OWNDEBUG EXAMPLE
OWNUTILS_OBJECTS=ownutils_example.opp ../ownutils.o
ownutils_example: $(OWNUTILS_OBJECTS)
	@$(LINK) -o $@ $^
	@echo "      [LD] $@"

# Rule for BINBAG EXAMPLE
BINBAG_OBJECTS=binbag_example.opp ../binbag.opp ../owndebug.o ../ownutils.o
binbag_example: $(BINBAG_OBJECTS)
	@$(LINK) -o $@ $^
	@echo "      [LD] $@"

# Rule for threadandsync EXAMPLE
THREADANDSYNC_OBJECTS=threadandsync_example.opp ../owndebug.o ../ownutils.o
threadandsync_example: $(THREADANDSYNC_OBJECTS)
	@$(LINK) -o $@ $^
	@echo "      [LD] $@"

# Rule for databacklog EXAMPLE
DATABACKLOG_OBJECTS=databacklog_example.opp \
		    ../owndebug.o ../ownutils.o ../gimutils.opp 
databacklog_example: $(DATABACKLOG_OBJECTS) ../databacklog.hpp
	@$(LINK) -o $@ $(DATABACKLOG_OBJECTS)
	@echo "      [LD] $@"

# Rule for Base64 EXAMPLE
BASE64_OBJECTS=base64_example.opp \
	       ../base64.opp ../owndebug.o ../ownutils.o
base64_example: $(BASE64_OBJECTS)
	@$(LINK) -o $@ $^
	@echo "      [LD] $@"

# Rule for bitOps EXAMPLE
BITOPS_OBJECTS=bitOps_example.o \
	       ../bitOps.o ../owndebug.o ../ownutils.o
bitOps_example: $(BITOPS_OBJECTS)
	@$(LINK) -o $@ $^
	@echo "      [LD] $@"

# Rule for datatypesizes EXAMPLE
DTS_OBJECTS=datatypesizes.o
datatypesizes_example: $(DTS_OBJECTS)
	@$(LINK) -o $@ $^
	@echo "      [LD] $@"

###############################################################################
###############################################################################
###############################################################################
# To clean up
clean:
	@rm -f \
	$(SETTINGS_OBJECTS) \
	$(OWNDEBUG_OBJECTS) \
	$(BINBAG_OBJECTS) \
	$(THREADANDSYNC_OBJECTS) \
	$(DATABACKLOG_OBJECTS) \
	$(BASE64_OBJECTS)
	@echo "      [CLEAN] <objects>"

realclean real-clean: clean
	@rm -f \
	settings_example \
	owndebug_example \
	binbag_example \
	threadandsync_example \
	databacklog_example \
	datatypesizes_example \
	bitOps_example \
	base64_example
	@echo "      [CLEAN] <executables>"

###############################################################################
###############################################################################

# Compile rule for all .o (.c) file
%.o:	%.c
	@$(COMPILEC) -o $@ $<
	@echo "      [CC] $<"

# Compile rule for all .opp (.cpp) files
%.opp:	%.cpp
	@$(COMPILECPP) -o $@ $<
	@echo "      [CXX] $<"

###############################################################################
